package com.admin.business.background.venusOaPrizepun;

import com.admin.business.background.approval.ApprovalDao;
import com.admin.business.background.approval.ApprovalService;
import com.admin.business.background.venusOaIncentive.VenusOaIncentiveDao;
import com.admin.business.background.venusOaPrizepunCheck.VenusOaPrizepunCheckDao;
import com.admin.business.background.venusOaWarning.VenusOaWarningService;
import com.admin.business.venusOrganizeEmployee.VenusOrganizeEmployeeService;
import com.admin.business.venusOrganizePost.VenusOrganizePostService;
import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import com.zmbsms.os.core.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description: 员工奖惩表
 * @create: 2023-11-06 15:51:40
 **/
@Service("venusOaPrizepunService")
@Slf4j
public class VenusOaPrizepunService{

    @Resource
    VenusOaPrizepunDao venusOaPrizepunDao;


    @Resource
    VenusOaPrizepunCheckDao venusOaPrizepunCheckDao;


    @Resource
     public ApprovalService approvalService;

    @Resource
    VenusOrganizeEmployeeService venusOrganizeEmployeeService;

    /**
     * 审批
     *
     * @param cdoRequest
     * @return
     */
    @OpenApi
    public Return updateIncentiveInfoByApproval(CDO cdoRequest) {

        CDO incentiveInfo = venusOaPrizepunDao.getIncentiveInfoById(cdoRequest.getString("id"));//奖惩信息
        String post_id = incentiveInfo.getString("post_id");
        //判断是否为跨级审批或是待我审批
        String check_id = incentiveInfo.getString("check_id");//待审批人user_id
        Integer check_grade = incentiveInfo.getInteger("check_grade");//所需审批等级，也可以查设置表信息，根据具体业务进行更改
        if (incentiveInfo.getInteger("prizepun_state") != 1) {
            return Return.valueOf(-1, "已有人审批，请勿再次操作");
        }

        CDO addCheck = new CDO();
        addCheck.set("pid", cdoRequest.getString("id"));
        addCheck.set("user_id", cdoRequest.getString("user_id"));
        addCheck.set("org_id",  cdoRequest.getString("org_id"));
        addCheck.set("check_state", cdoRequest.getString("check_state"));
        addCheck.set("remark", cdoRequest.getString("remark"));
        venusOaPrizepunCheckDao.addCheckInfo(addCheck);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CDO update = new CDO();
        update.set("id", cdoRequest.getString("id"));
        update.set("update_time", sdf.format(new Date()));
        //update_time即为最后一次审批的时间
        if (cdoRequest.getString("check_state").equals("1")) {
            //拒绝了直接修改该条数据状态为驳回
            update.set("prizepun_state", 4);
            venusOaPrizepunDao.updateVenusOaPrizepunById(update);
            return Return.SUCCESS;
        }

        //成功审批
        Integer dataState = 3;//1.待审批 3.已通过
        //判断本次审批成功后是否还要审批
        Boolean whetherToApprove = approvalService.verifyDataInfoState(cdoRequest.getString("user_id"), check_grade, post_id);//是否还需要审批
        if (whetherToApprove) {
            //还有审批
            //查询审批人有无上级
            String checkId = approvalService.getBossUserIdByUserIdAndOrgId(cdoRequest.getString("user_id"),  cdoRequest.getString("org_id"));//申请人上级
            if (!StringUtils.isEmpty(checkId)) {
                dataState = 1;
                update.set("check_id", checkId);
            }
        }

        update.set("prizepun_state", dataState);
        venusOaPrizepunDao.updateVenusOaPrizepunById(update);
        if (dataState == 3) {
            //添加记录至 venus_oa_incentive
            String msg = this.addPrizePunInfo(incentiveInfo);
            System.out.println(msg);
            // 2022-9-8 员工奖惩关联警示 zwl
            String employee_alerts = cdoRequest.getString("employee_alerts");
            if (!StringUtils.isEmpty(employee_alerts)) {
                if (employee_alerts.equals("0")) {//0是 1 否
                    this.addWarningInfo(incentiveInfo);
                }
            }

            //2022-8-18，添加惩罚连坐信息至 venus_oa_incentive
            String prizepun_type = incentiveInfo.getString("prizepun_type");//1.奖励，2.惩罚
            String nowTimeStr = sdf.format(new Date());
            if (prizepun_type.equals("2")) {
                String add_code = incentiveInfo.getString("add_code");
//                if (!StringUtils.isEmpty(add_code)) {
//                    CDO setInfo = venusOaPrizepunMapper.getIncentiveSetInfoByOrgId(incentiveInfo.getString("orgId"));//设置的员工奖惩审批信息
//                    List<String> userIdList = venusOaPrizepunMapper.getUserIdListByAddCode(add_code);
//                    CDO request = new CDO();
//                    request.set("orgId", incentiveInfo.getString("orgId"));
//                    request.set("user_id", cdoRequest.getString("user_id"));
//                    request.set("post_id", post_id);
//                    request.set("money", incentiveInfo.getString("prizepun_money"));
//                    request.set("checkUserIdList", userIdList);
//                    //查询公司的设置信息
//                    String joint_one_per = null;
//                    String joint_two_per = null;
//                    String joint_three_per = null;
//                    if (setInfo != null) {
//                        joint_one_per = setInfo.getString("joint_one_per");
//                        joint_two_per = setInfo.getString("joint_two_per");
//                        joint_three_per = setInfo.getString("joint_three_per");
//                    }
//
//                    request.set("joint_one_per", joint_one_per);
//                    request.set("joint_two_per", joint_two_per);
//                    request.set("joint_three_per", joint_three_per);
//                    Return jointPunishInfo = this.getJointPunishInfo(request);
//                    CDO resultInfo = jointPunishInfo.getData();
//                    List<CDO> resultList = new ArrayList<>();
//                    List<Object> datalist = resultInfo.getObjectList("datalist");
//                    CDO result;
//                    for (Object objectResult : datalist) {
//                        result = CDO.parseObject(objectResult);
//                        resultList.add(result);
//                    }
//
//                    for (CDO cdo : resultList) {
//                        CDO userInfo = cdo.getCDO("userInfo");
//                        String money = cdo.getString("money");//处罚金额
//                        String sit_together_userId = userInfo.getString("user_id");//连坐对象 user_id
//                        String postId = venusOaPrizepunMapper.getPostIdByUserIdAndOrgId(sit_together_userId, incentiveInfo.getString("orgId"));
//                        //org_id和user_id查个岗位id
//                        CDO addRecord = new CDO();
//                        addRecord.set("user_id", sit_together_userId);
//                        addRecord.set("orgId", incentiveInfo.getString("orgId"));
//                        addRecord.set("post_id", postId);
//                        addRecord.set("by_user_id", cdoRequest.getString("user_id"));
//                        addRecord.set("incentive_type", 2);
//                        addRecord.set("time", nowTimeStr);
//                        addRecord.set("data_remark", "连带处罚");
//                        addRecord.set("remark", cdoRequest.getString("remark"));
//                        addRecord.set("pid", cdoRequest.getString("id"));
//                        addRecord.set("money", money);
//                        addRecord.set("add_type", 2);
//                        addRecord.set("sit_together_id", add_code);
//                        //校验数据是否存在
//                        int count = venusOaPrizepunMapper.verifyDataByUserIdAndAddType(sit_together_userId, add_code);
//                        if (count == 0) {
//                            this.addIncentiveInfoPublicMethod(addRecord);
//                        }
//                    }
//
//                }
            }
        }
        return Return.SUCCESS;
    }

    @Resource
    VenusOaWarningService venusOaWarningService;

    //员工警告记录
    public void addWarningInfo(CDO incentiveInfo) {
        CDO caution = new CDO();
        caution.set("by_user_id", incentiveInfo.getLong("user_id"));//添加人
        caution.set("org_id", incentiveInfo.getLong("orgId"));//公司id
        caution.set("post_id", incentiveInfo.getLong("post_id"));//岗位id
        caution.set("user_id", incentiveInfo.getLong("by_user_id"));//被添加人
        caution.set("warning_type", "现场处理");//警示类型
        caution.set("warning_note", incentiveInfo.getString("prizepun_explain"));//警示备注
        caution.set("incentive_type", incentiveInfo.getString("prizepun_type"));//奖惩( 1 奖励 2 惩罚 3 警告)
        caution.set("warning_money", incentiveInfo.getString("prizepun_money"));//金额
        caution.set("warning_image", incentiveInfo.getString("prizepun_images"));//图片
        venusOaWarningService.addVenusOaWarning(caution);
    }

    //员工处罚记录
    public String addPrizePunInfo(CDO incentiveInfo) {
        CDO addRecord = new CDO();
        addRecord.set("user_id", incentiveInfo.getString("by_user_id"));
        addRecord.set("orgId", incentiveInfo.getString("orgId"));
        addRecord.set("post_id", incentiveInfo.getString("post_id"));
        addRecord.set("by_user_id", incentiveInfo.getString("user_id"));
        addRecord.set("incentive_type", incentiveInfo.getString("prizepun_type"));
        addRecord.set("time", incentiveInfo.getString("begin_time"));
        addRecord.set("data_remark", "现场处理");
        addRecord.set("remark", incentiveInfo.getString("prizepun_explain"));
        addRecord.set("pid", incentiveInfo.getString("id"));
        addRecord.set("money", incentiveInfo.getString("prizepun_money"));
        addRecord.set("add_type", 1);
        String msg = this.addIncentiveInfoPublicMethod(addRecord);
        return msg;
    }


    @Resource
    ApprovalDao approvalDao;

    @Resource
    VenusOaIncentiveDao venusOaIncentiveDao;

    /**
     * 公共添加奖惩信息
     *
     * @param cdoRequest
     * @return
     */
    public String addIncentiveInfoPublicMethod(CDO cdoRequest) {

        //2022-8-17，新增连坐处罚，只限于手动处罚，防止影响之前功能，嫁接新增了连坐处罚关联id
        String msg = "添加成功";
        String orgId = cdoRequest.getString("orgId");//总公司id
        String user_id = cdoRequest.getString("user_id");//奖惩对象user_id
        String by_user_id = cdoRequest.getString("by_user_id");//操作人id，不必传
        String post_id = cdoRequest.getString("post_id");//奖惩对象岗位id，尽量传，可不必传
        Integer incentive_type = cdoRequest.getInteger("incentive_type");//奖惩类型 1.奖励，2.惩罚
        String time = cdoRequest.getString("time");//时间 年-月
        String data_remark = cdoRequest.getString("data_remark");//数据备注  例子：循环计划未完成，考勤缺卡........
        String remark = cdoRequest.getString("remark");//自定义备注
        String pid = cdoRequest.getString("pid");//关联的id，例子：报修主键id，工作id，奖惩id.....
        String money = cdoRequest.getString("money");//金额
        Integer add_type = cdoRequest.getInteger("add_type");//添加类型 1.手动 2.系统
        if (StringUtils.isEmpty(orgId)) {
            return "总公司orgId不可为空";
        }
        if (StringUtils.isEmpty(user_id)) {
            return "操作人对象user_id不可为空";
        }
        if (incentive_type == null) {
            return "奖惩类型incentiveType不可为空";
        }
        if (StringUtils.isEmpty(time)) {
            return "时间time不可为空";
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 指定时间格式
            cdoRequest.set("time", sdf.format(sdf.parse(time)));
        } catch (Exception e) {
            e.printStackTrace();
            return "time参数错误";
        }


        if (StringUtils.isEmpty(data_remark)) {
            return "数据备注dataRemark不可为空";
        }
        if (StringUtils.isEmpty(money)) {
            return "金额money不可为空";
        }
        if (add_type == null) {
            return "add_type不可为空";
        }
        String org_id = null;//所属公司id
        String pro_id = null;//项目id
        String dept_id = null;//部门id
        if (!StringUtils.isEmpty(post_id)) {
            CDO postInfo = approvalDao.getPostInfoByPostId(post_id);
            if (postInfo != null) {
                org_id = StringUtils.isEmpty(postInfo.getString("org_id")) ? null : postInfo.getString("org_id");
                pro_id = StringUtils.isEmpty(postInfo.getString("pro_id")) ? null : postInfo.getString("pro_id");
                dept_id = StringUtils.isEmpty(postInfo.getString("dept_id")) ? null : postInfo.getString("dept_id");
            }
            cdoRequest.set("org_id", org_id);
            cdoRequest.set("pro_id", pro_id);
            cdoRequest.set("dept_id", dept_id);
            //根据user_id和post_id查员工编号
            CDO employeeCode = approvalDao.getEmployeeCodeByUserIdAndPostId(user_id, post_id);
            cdoRequest.set("employee_code", employeeCode.getString("employee_code"));
        }
        //校验数据是否重复
        venusOaIncentiveDao.addVenusOaIncentive(cdoRequest);
        //2022-11-3 zwl 员工受到奖惩后会受到系统发的消息
        CDO message = new CDO();
        message.set("by_user_id", user_id);//接收人
        message.set("sendType", 2);//发送类型  1.助业网  2.员工端||业主端
        message.set("text", "《系统消息》由于您" + cdoRequest.getString("remark") + "受到" + (cdoRequest.getString("incentive_type").equals("1") ? "奖励" :
                cdoRequest.getString("incentive_type").equals("2") ? "扣款" : "警告") + "，详情请到助业网进行查看。(勿回)");//内容
//        VenusOaSysMessageService.addSystemMessages(message); 短信消息提醒
        return msg;
    }




}
